
import { globalUtils } from '../../../common/js/global-utils/global-utils-all';
import { uiUtils } from '../../../common/js/ui-utils/ui-utils-all';
import Pure from 'pure-frontend';

/* SOURCE-CODE-START */

"use strict";


Pure.app.viewMain('example2-search', function (scope, view) {

  var jqTable = view.$find('table');
  var jqCreateBtn = view.$ui('createBtn');
  var jqBatchDeleteBtn = view.$ui('batchDeleteBtn');
  var jqSearchBtn = view.$ui('searchBtn');
  var jqCreateDateFromInput = view.$ui('createdDateFrom');
  var jqCreatedDateToInput = view.$ui('createdDateTo');

  // 点击查询按钮事件
  jqSearchBtn.on('click', scope.clickSearchBtnFn);
  // 点击新建按钮事件
  jqCreateBtn.on('click', scope.clickCreateBtnFn);
  // 点击批量删除按钮事件
  jqBatchDeleteBtn.on('click', scope.clickBatchDeleteBtnFn);

  // 初始日期选择器
  uiUtils.datePicker.init(jqCreateDateFromInput);
  uiUtils.datePicker.init(jqCreatedDateToInput);

  // 初始表格
  uiUtils.dataGrid.init(jqTable, {
    url: 'api/example/pageData',
    columns: [
      {},
      {
        field: 'id'
      },
      {
        field: 'exampleName'
      },
      {
        field: 'remark'
      },
      {
        field: 'enableFlagDesc'
      },
      {
        field: 'createdDate',
        formatter: formatCreateDateColumn
      },
      {
        field: 'operate',
        formatter: formatOprationColumn,
        events: {
          'click .view': scope.clickViewOperationFn,
          'click .delete': scope.clickDeleteOperationFn,
          'click .edit': scope.clickEditOperationFn
        }
      }
    ]
  });

  function formatCreateDateColumn(value, row, index) {
    return globalUtils.formatYearSecond(globalUtils.parseDateTimeWithZone(value));
  }

  function formatOprationColumn(value, row, index) {
    return [
      '<a class="view" href="javascript:void(0)">',
      '<span>${t("ui.example.example2.operation.view")}</span>',
      '</a>',
      '&nbsp;',
      '<a class="edit" href="javascript:void(0)">',
      '<span>${t("ui.example.example2.operation.edit")}</span>',
      '</a>',
      '&nbsp;',
      '<a class="delete" href="javascript:void(0)">',
      '<span>${t("ui.example.example2.operation.delete")}</span>',
      '</a>'
    ].join('');
  }

  // 表格复选框改变时重新计算是否禁用按钮
  uiUtils.dataGrid.onCheckBoxChange(jqTable, function (rows) {
    var disabled = (rows.length == 0);
    jqBatchDeleteBtn.prop('disabled', disabled);
  });

  // 弹出视图时重新刷新表格的数据
  scope.onViewPop = function () {
    uiUtils.dataGrid.refresh(jqTable);
  };

  scope.getAppView = function () {
    return view;
  };

  scope.getJqTable = function () {
    return jqTable;
  };

  scope.getJqBatchDeleteBtn = function () {
    return jqBatchDeleteBtn;
  };

});

/* SOURCE-CODE-END */
